package net.juntuo.ffmpeg;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import com.arthenica.mobileffmpeg.FFmpeg;

import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_CANCEL;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_SUCCESS;

public class FFMpeg extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {

        if (action.equals("exec")) {

            //TODO: call ffmpeg-mobile and do the processing ...
            //FFmpeg.execute("-i file1.mp4 -c:v mpeg4 file2.mp4");
            //https://github.com/tanersener/mobile-ffmpeg/wiki/Android

            int rc = FFmpeg.execute(data.getString(0));

            if (rc == RETURN_CODE_SUCCESS) {
                callbackContext.success("Command execution completed successfully");
            } else if (rc == RETURN_CODE_CANCEL) {
                callbackContext.error("Command execution cancelled by user");
            } else {
                callbackContext.error("Command execution error");
            }
            return true;
        }

        if (action.equals("stop")) {
            FFmpeg.cancel();
            return true;
        }

        return false;
    }
}
